/*
 * @Author: superRice
 * @Date: 2025-03-11 18:02:07
 * @LastEditTime: 2025-05-27 18:53:11
 * @FilePath: \threejs-testing-dedicated\examples\envRotate\index.js
 * @Description:
 * -----愿你走过所有风雨，归来仍少年----
 * Copyright (c) 2025 by 造梦星空, All Rights Reserved.
 */
import * as TE from 'TEngine';
import * as THREE from 'three';
import { Pane } from 'tweakpane';
const TS = new TE.TEngine();
TS.camera.up.set(0, 0, 1);

TS.camera.position.set(10, 10, 10);
TS.camera.lookAt(0, 0, 0);
TS.camera.updateProjectionMatrix();

const axes = new THREE.AxesHelper(5);
TS.scene.add(axes);
function loadAndRotateImage(url, angle) {
  return new Promise((resolve, reject) => {
    const img = new Image();
    img.crossOrigin = 'anonymous'; // 处理跨域图片
    img.onload = function () {
      const radians = (angle * Math.PI) / 180;

      // 计算旋转后图片的外接矩形尺寸
      const sin = Math.abs(Math.sin(radians));
      const cos = Math.abs(Math.cos(radians));
      const newWidth = img.width * cos + img.height * sin;
      const newHeight = img.width * sin + img.height * cos;

      // 创建新的画布
      const canvas = document.createElement('canvas');
      canvas.width = newWidth;
      canvas.height = newHeight;
      const ctx = canvas.getContext('2d');

      // 移动原点到画布中心，进行旋转
      ctx.translate(newWidth / 2, newHeight / 2);
      ctx.rotate(radians);

      // 绘制图片
      ctx.drawImage(img, -img.width / 2, -img.height / 2);

      // 转换为 Base64
      const base64String = canvas.toDataURL('image/png');
      resolve(base64String);
    };

    img.onerror = function () {
      reject(new Error('图片加载失败'));
    };

    img.src = url;
  });
}

//开始加载天空盒

const initCube = async function () {
  'px.png', 'nx.png', 'py.png', 'ny.png', 'pz.png', 'nz.png';

  const cubeMap = new THREE.CubeTextureLoader().load([
    await loadAndRotateImage('/skyboxRotate/px.png', -90),
    await loadAndRotateImage('/skyboxRotate/nx.png', 90),
    await loadAndRotateImage('/skyboxRotate/nz.png', 180),
    await loadAndRotateImage('/skyboxRotate/pz.png', 0),
    await loadAndRotateImage('/skyboxRotate/py.png', 0), //顶
    await loadAndRotateImage('/skyboxRotate/ny.png', 180), //底
  ]);

  //nx旋转90

  TS.scene.background = cubeMap;
  TS.scene.environment = cubeMap;
};
initCube();
//开始创建一个小球

const cube = new THREE.SphereGeometry(1, 32, 32);
const material = new THREE.MeshStandardMaterial({ color: 0xffffff, roughness: 0, metalness: 1 });
const sphere = new THREE.Mesh(cube, material);
TS.scene.add(sphere);
